﻿using System.Linq;
using Microsoft.AspNetCore.Http;

namespace CoreDemo.Extensions
{
   public static class HttpContextExtension
    {
        public static string GetRemoteIp(this HttpContext ctx,string forwardedStr= "X-Forwarded-For")
        {
            var ip = ctx.Request.Headers[forwardedStr].FirstOrDefault();
            if (string.IsNullOrWhiteSpace(ip))
            {
                ip = ctx.Connection.RemoteIpAddress.ToString();
            }

            return ip;
        }
    }
}
